Determine the
number of sentences in the given text. Assume that a sentence ends with one of
the following symbols: ‘.’ (period), ‘!’ (exclamation mark), or ‘?’ (question
mark). It is assumed that the text does not contain combinations of symbols
such as “...”, “!!!”, or “?!”.
Input. A single string
containing text composed of Latin letters, punctuation marks, and spaces. The
string length does not exceed 200 characters.
Output. Print the number of
sentences in the text.
Sample input |
Sample output |
Hello, world! |
1 |
string
Algorithm
analysis
To solve
the problem, you need to count the number of characters ‘.’, ‘!’ and ‘?’ in the given string.
Algorithm implementation
Read the input string.
getline(cin,
s);
The variable res is used to count the
occurrences of the characters ‘.’, ‘!’ and ‘?’ in the
string s.
res = 0;
for (i = 0; i < s.size(); i++)
if (s[i] == '.' || s[i] == '!' || s[i] == '?') res++;
Print the answer.
printf("%d\n", res);
Python implementation
Read the input string.
s = input()
The variable res is used to count the
occurrences of the characters ‘.’, ‘!’ and ‘?’ in the
string s.
res = 0
for x in s:
if x in ".!?": res += 1
Print the answer.
print(res)
Python implementation – sum for
Read the input string.
s = input()
The variable res is used to count the
occurrences of the characters ‘.’, ‘!’ and ‘?’ in the
string s.
res = sum(s.count(char) for char in '.!?')
Print the answer.
print(res)